//
//  HKLTabBarController.m
//  charity
//
//  Created by san_xu on 2016/12/27.
//  Copyright © 2016年 com.charity.huakala. All rights reserved.
//

#import "HKLTabBarController.h"
#import "HKLNavigationController.h"
#import "RABookMallController.h"
#import "RABookrackController.h"
#import "RASearchController.h"
#import "RCTEntryViewController.h"

@interface HKLTabBarController () <UITabBarControllerDelegate, KAAdInterstitialDelegate>

@property (nonatomic, strong) KAAdInterstitial *interstitial;

@end

@implementation HKLTabBarController {
  BOOL _alreadyPop;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
  _alreadyPop = NO;
  
    //初始化界面
    [self initUserInterface];
}

//初始化界面
- (void)initUserInterface {
    //去掉底部tabbar的半透明效果
    self.tabBar.translucent = NO;
    
    //添加子控制器
    RABookrackController *homeController = [[RABookrackController alloc]init];
    [self addChildCtrl:homeController title:@"书架" image:@"Bookrack"];

    RABookMallController *charityRingController = [[RABookMallController alloc]init];
    [self addChildCtrl:charityRingController title:@"书城" image:@"BookMall"];
  
//    RASearchController *mineController = [[RASearchController alloc]init];
  RCTEntryViewController *entryViewController = [[RCTEntryViewController alloc]init];
    [self addChildCtrl:entryViewController title:@"分类" image:@"Category"];
  
  
  self.delegate = self;
}


//  添加子控制器
- (void)addChildCtrl:(UIViewController *)childCtrl title:(NSString *)title image:(NSString *)imageName {
    
    //设置导航栏和tabbaritem的标题
    childCtrl.navigationItem.title = title;
    childCtrl.tabBarItem.title = title;
    
    //设置item的图片
    [childCtrl.tabBarItem setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    UIImage * selectedImg = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_Selected",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [childCtrl.tabBarItem setSelectedImage:selectedImg];
    
    //设置item的文字
    NSMutableDictionary * normalDict = [NSMutableDictionary dictionary];
    normalDict[NSForegroundColorAttributeName] = UIColorHex(bfbfbf);
    [childCtrl.tabBarItem setTitleTextAttributes:normalDict forState:UIControlStateNormal];//默认
    
    NSMutableDictionary * selectedDict = [NSMutableDictionary dictionary];
    selectedDict[NSForegroundColorAttributeName] = UIColorHex(f21f2b);
    [childCtrl.tabBarItem setTitleTextAttributes:selectedDict forState:UIControlStateSelected];//高亮
    
    //添加导航控制器
    HKLNavigationController *navigationController = [[HKLNavigationController alloc]initWithRootViewController:childCtrl];
    
    [self addChildViewController:navigationController];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  
  NSUInteger selectedIndex = tabBarController.selectedIndex;
  if (selectedIndex == 1 && !_alreadyPop) {
    _alreadyPop = YES;
    [self showInsertAdvert];
  }
  
}

// 插屏广告
- (void)showInsertAdvert {
  
  self.interstitial = [[KAAdInterstitial alloc] initWithSlot:@"ENAORRGW" delegate:self];
  [self.interstitial load];
  
}

#pragma mark - KAAdInterstitialDelegate
- (void)interstitialAdLoadDidSuccess:(nonnull KAAdInterstitial *) interstitialAd {
  
  if (self.view.window) {
    [self.interstitial presentFromRootViewController:self];
    NSLog(@"插屏广告插屏广告");
  }
  
}

- (void) interstitialAdLoadDidFailForSlot:(nonnull NSString *) interstitialAdSlot
                                withError:(nonnull NSError *) interstitialAdStatus {
}

- (void)showPopAd {
  
  // 一天一次判断
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  
  NSString *ageDateString = [userDefaults objectForKey:@"prePopAdDate"];
  NSString *nowDateString = [dateFormatter stringFromDate:[NSDate date]];
  
  if (ageDateString == nil) ageDateString = @"";
  
  BOOL sameDateFlag = NO;
  if (![ageDateString isEqualToString:nowDateString]) { // 假如不是同一天，更新存储的日期，并且把flag置为YES
    [userDefaults setObject:nowDateString forKey:@"prePopAdDate"];
    [userDefaults synchronize];
    sameDateFlag = YES;
  }
  
  NSLog(@"日期比较  之前：%@ 现在：%@",ageDateString, nowDateString);
  if (sameDateFlag) {
    NSLog(@"不是同一天");
  }
  else {
    NSLog(@"同一天");
  }
  
}

@end
